﻿using System.Text;

namespace EnpassVaultParser
{
    internal static class Program
    {
        public static string version = "1.0.0";
        public static Form1 form1 = new();
        public static string openFilePath = "";
        public static string openFileFolderPath = "";
        public static string openData = "";
        public static int itemCount = 0;

        public static List<string> openDataList = [];
        public static List<List<string>> processedDataList = [];

        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
            Application.Run(form1);
        }

        // 读取文件
        public static async Task<string> ReadFile(string filePath)
        {
            var inputStream = new StreamReader(filePath, Encoding.UTF8);
            var input = await inputStream.ReadToEndAsync();
            inputStream.Close();
            return input;
        }

        // 预处理文本
        public static List<string> PreProcessData(string data)
        {
            string[] temp = data.Split(['\r', '\n']);
            List<string> dataList = [];

            // 去除空行和制表符
            foreach (var line in temp)
            {
                if (line != "" && !line.Contains("备注："))
                {
                    if (line.Contains('\t'))
                    {
                        dataList.Add(line.Trim(['\t']) + "\n");
                    }
                    else
                    {
                        dataList.Add(line + "\n");
                    }
                }
            }

            return dataList;
        }

        // 标记处理预处理后的数据
        public static List<string> MarkAndProcess(List<string> dataList)
        {
            bool ifFirstIndex = true;
            List<int> startAndEndIndex = [];
            List<string> outPutData = [];

            // 标记开始和结束序号
            for (int i = 0; i < dataList.Count; i++)
            {
                if (dataList[i].Contains("标题："))
                {
                    if (ifFirstIndex)
                    {
                        startAndEndIndex.Add(i);
                        ifFirstIndex = false;
                    } else
                    {
                        startAndEndIndex.Add(i - 1);
                        startAndEndIndex.Add(i);
                    }
                }
            }
            // 最后一组的结尾序号
            startAndEndIndex.Add(dataList.Count - 1);

            // 区分开始和结束序号, 然后准备输出
            bool mark = true;
            for (int i = 0 ; i < startAndEndIndex.Count; i++)
            {
                int startIndex = 0;
                int endIndex = 0;
                if (mark)
                {
                    startIndex = startAndEndIndex[i];
                    endIndex = startAndEndIndex[i + 1];
                    mark = false;
                } else
                {
                    mark = true;
                    continue;
                }

                string itemMerge = "";
                // 按项拼接
                for (int j = startIndex; j <= endIndex; j++)
                {
                    itemMerge += dataList[j].ToString();
                }

                // 添加至输出列表
                outPutData.Add(itemMerge);
            }

            return outPutData;
        }

        // 将数据列表处理为嵌套列表
        public static List<List<string>> ProcessDataToList(List<string> dataList)
        {
            List<List<string>> outPutData = [];

            foreach (string item in dataList)
            {
                List<string> dataItem = [];

                // 按行分割
                string[] inputString = item.Split(['\r', '\n']);

                foreach (string line in inputString)
                {
                    if (line.Length > 0)
                    {
                        dataItem.Add(line);
                    }
                }

                outPutData.Add(dataItem);
            }

            return outPutData;
        }

        // 拆分文本
        public static string[] SplitTextInLine(string text)
        {
            if (text.Contains(" : "))
            {
                text = text.Replace(" : ", "©");
            } else if (text.Contains('：'))
            {
                text = text.Replace("：", "©");
            }

            string[] output = text.Split('©');
            return output;
        }
    }
}